/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.ibs;

import it.enrollement.System_Param;
import it.user.Param_ibdsa;
import it.utils.Hash;
import java.util.Random;
import jpair.api.FieldElement;
import jpair.pairing.BigInt;
import jpair.pairing.Point;

/**
 *
 * @author Francesco
 */
public class IB_DSA {
    
    
     System_Param parameter;
    BigInt modf,mod;
   
     
    public IB_DSA(System_Param param) {

        parameter = param;
        modf = parameter.getE().getCurve().getField().getOrder();
         mod = parameter.getE().getGroupOrder();
    }

    public IB_DSA() {
    }
    
    
     /**
     *
     * @param message The message to sign
     * @param SignatureKey The user signature private key
     * @return The message signature
     */
    public Param_ibdsa sign(String message, BigInt kdsa) {

        BigInt h1m,sum1,sum2,sum3=BigInt.ZERO;
        Point h1p, signature = null;
        Param_ibdsa sign= new Param_ibdsa();
        
        BigInt k,r;
        Point R;
        Random rnd;
        Hash h = new Hash();
        try {
            
            // Random value
               rnd = new Random();
             // Private Master key s
             k = new BigInt(parameter.getE().getGroupOrder().bitLength(), rnd);
        
        
            R =  parameter.getE().getCurve().multiply(parameter.getP(), k);
            
            r= h.h1(R.toString().getBytes(), parameter.getE().getCurve().getField());
            
            // H1(m)
            
            h1m = h.h1(message.getBytes("UTF-8"), parameter.getE().getCurve().getField());
            
            sum1= kdsa.multiply(r);
            
            sum2= sum1.add(h1m);
            
            sum3 = sum2.multiply(k.modInverse(mod));
            
          
            sign.setR(R);
            sign.setSigma(sum3);
             
            
        } catch (Exception c) {
            c.printStackTrace();

        }

        return sign;

    }
    
    
       /**
     *
     * @param message the message
     * @param identity identity of signer
     * @param signature the signature of message
     * @return true if signature is verified
     */
    public boolean vrf(String message, String identity, Param_ibdsa signature, Point Tid) {
        boolean res = false;
        BigInt r, h1m,invsig=BigInt.ZERO;
        Point eG,rT, eGrT,eGrTs;
        Hash h = new Hash();
        try {
            
               r = h.h1(signature.getR().toString().getBytes(), parameter.getE().getCurve().getField());
               
           
                
                 h1m = h.h1(message.getBytes("UTF-8"), parameter.getE().getCurve().getField());
       
                
                eG= parameter.getE().getCurve().multiply(parameter.getP(), h1m);
      
                
                 rT = parameter.getE().getCurve().multiply(Tid, r);
      
                 
                 eGrT=parameter.getE().getCurve().add(eG, rT);
                 
         
                 
                   //( 1 / (sigma))
           invsig = signature.getSigma().modInverse(mod);
            
       
                 
             eGrTs = parameter.getE().getCurve().multiply(eGrT, invsig);
    

            if (signature.getR().equals(eGrTs)) {
                res = true;
              System.out.println("Signature Verification = True");

            } else {
              System.out.println("Signature Verification = False");
                res = false;
            }

        } catch (Exception c) {
            c.printStackTrace();

        }

        //    System.out.println("Signature Verification for User "+identity+" ="+res);   

        return res;

    }
    
}
